﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services;
using ESF.General.Entity;
using ESF.General;
using ESF.General.Text;
using ESF.SFT.Entities;
using ESF.WCF;
using ESF.SFT.Proxies;
using ESF.SFT.Service.Messages;
using System.Xml;
using System.Configuration;
using ESF.SFT.Service.FileMetadataPublisher;
using ESF.SFT.Service.FileReceiveLogger;
using System.Web.Configuration;
using System.IO;
using System.Web;
using ESF.SFT.Service.FileReplicator;
using System.ServiceModel.Activation;

#endregion


namespace ESF.SFT.Service
{
	[ExceptionBehaviorAttribute]
	[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
	public class ReceiverGeneral : IReceiverGeneral
	{
		#region WS

		private FacCliente _SISPROSegWS;

		private FacCliente SISPROSegWS
		{
			get
			{
				if (_SISPROSegWS == null)
				{
					_SISPROSegWS = new FacCliente();
				}

				return _SISPROSegWS;
			}
		}

		#endregion


		#region Methods

		public string GetClientCurrentVersion ()
		{
			return ReadConfig.GetAppSetting ("ClientMininumVersion");
		}


		public Couple<string, string>[] GetConfiguration()
		{
			string minimunVersion;
			string clientVersion;


			minimunVersion = GetClientCurrentVersion ();

			clientVersion = WCFHelper.GetHeader<string> ("ClientVersion");

			if (clientVersion != minimunVersion)
			{
				throw new GenericInfoException (
					string.Format ("La versión de su Aplicación es la {0} pero la requerida por el servicio es {1}\n\nActualice esta Aplicación (opcionalmente puede reinstalarla) e intente de nuevo.",
						clientVersion, minimunVersion));
			}

			return new Couple<string, string>[]
			{
				new Couple<string,string> ("SignUpURL", ReadConfig.GetAppSetting("SignUpURL")),
				new Couple<string,string> ("ForgetPasswordURL", ReadConfig.GetAppSetting("ForgetPasswordURL")),
				new Couple<string,string> ("ChangePasswordURL", ReadConfig.GetAppSetting("ChangePasswordURL")),
				new Couple<string,string> ("FirstTimeOrPasswordReset_Code", ReadConfig.GetAppSetting("FirstTimeOrPasswordReset_Code")),
				new Couple<string,string> ("ExpiredPassword_Code", ReadConfig.GetAppSetting("ExpiredPassword_Code")),
				new Couple<string,string> ("ChangePassword_Code", ReadConfig.GetAppSetting("ChangePassword_Code")),
				new Couple<string,string> ("Culture", ReadConfig.Culture),
				new Couple<string,string> ("ClientMininumVersion", minimunVersion),
				new Couple<string, string> ("CertificationAuthorities", ReadConfig.GetAppSetting ("CertificationAuthorities"))
			};
		}


		public Couple<string, string>[] StartSession(string pUserName, string pUserPassword)
		{
			string ssoProvider;
			string ticket;

			XmlDocument xmlTemp;


			ssoProvider = ReadConfig.GetAppSetting("SSOProvider");

			switch (ssoProvider)
			{
				case "ESF":
					throw new Exception("Proveedor de Seguridad ESF pendiente de soporte");

				case "SISPRO":
					ticket = SISPROSegWS.AutenticarUsuarioTicketI(pUserName, pUserPassword);

					xmlTemp = new XmlDocument();

					xmlTemp.LoadXml(SISPROSegWS.ConsultarDetalleUsuario(pUserName));

					return new Couple<string, string>[]
					{
						new Couple<string,string> ("Ticket", ticket),

						new Couple<string,string> ("UserFullName", xmlTemp.DocumentElement["NombreCompleto"].InnerText),
						new Couple<string,string> ("UserEmail", xmlTemp.DocumentElement["CorreoElectronico"].InnerText),
						
						new Couple<string,string> ("EnterpriseName", xmlTemp.DocumentElement["Entidad.Nombre"].InnerText),
						new Couple<string,string> ("EnterpriseIdentificationType", xmlTemp.DocumentElement["Entidad.TipoIdentificacion"].InnerText),
						new Couple<string,string> ("EnterpriseIdentificationNumber", xmlTemp.DocumentElement["Entidad.NumeroIdentificacion"].InnerText),
						new Couple<string,string> ("EnterpriseEmail", xmlTemp.DocumentElement["Entidad.CorreoElectronico"].InnerText)
					};

				default:
					throw new Exception(string.Format("Proveedor de Seguridad {0} no soportado", ssoProvider));
			}
		}


		public List<FileRegisterValidatorSet> GetFileRegisterValidatorSets(string pSessionTicket, string pUserName)
		{
			return new MetadataPublisherClient().GetAll(pUserName);
		}


		public BrowseResults<FileReceiveLog> GetFileUploadHistory(FileReceiveLog pSample, PaggingInfo pPagInfo)
		{
			return new FileReceiveLoggerClient().GetFileUploadHistory(pSample, pPagInfo);
		}


		public Couple<string, string>[] GetFileUploadResumen(string pSessionTicket, string pUserName)
		{
			return new FileReceiveLoggerClient().GetFileUploadResumen(pSessionTicket, pUserName).ToArray();
		}

		#endregion
	}
}
